<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	
</body>
<script>

//遵循FIFO原则
	function Queue(){
		this.items=[];
		//添加元素
		this.enqueue=function(element){
			this.items.push(element)
		};
		//移除元素
		this.dequeue=function(){
			return this.items.shift();
		};

		//返回最前面的元素
		this.front=function(){
			return this.items[0];
		};

		//判断数组是不是空
		this.isEmpty=function(){
			return this.items.length==0;
		};

		//清空数组
		this.clear=function(){
			this.items=[];
		};
		//返回数组的长度
		this.size=function(){
			return this.items.length;
		};

		//打印数组
		this.print=function(){
			console.log(this.items.toString());
		}

	}

	//优先队列
	function PriorityQueue(){
		
		function QueueElement(element,priority){
			this.element=element;
			this.priority=priority;
		}
		this.enqueue=function(element,priority){
			var queueElement=new QueueElement(element,priority);

			if(this.isEmpty()){
				this.items.push(queueElement);
			}else{
				var added=false;
				for(var i=0;i<this.items.length;i++){
					if(queueElement.priority<this.items[i].priority){
						this.items.splice(i,0,queueElement);
						added=true;
						break;
					}
				}

				if(!added){
					this.items.push(queueElement);
				}
			}
		}
	}

	PriorityQueue.prototype=new Queue();

	var  priorityQueue=new PriorityQueue();
	priorityQueue.enqueue('john',2);
	priorityQueue.enqueue('jack',1);
	priorityQueue.enqueue('Camila',1);
	priorityQueue.print();

</script>
</html>